package com.century.sjt.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.entity.ChangeOrderInfo;
import com.century.sjt.entity.MyOrderInfo;
import com.century.sjt.util.TipUtil;

import java.util.List;

/**
 * @author:周玲波
 * @Description: 用户订单详情适配器
 * @date : 2016/8/30 20:22
 */
public class MyOrderDetailsAdapter extends BaseAdapter {
    Context mContext;
    List<MyOrderInfo> mList;
    LayoutInflater inflater;
    private Handler mHandler;
    ChangOrderListenner mChangClick;
    double amount;//每次增加的总额
    double sum;//修改前的总额
    ChangeOrderInfo mInfo;

    public MyOrderDetailsAdapter(Context mContext, List<MyOrderInfo> mList, Handler mHandler, ChangeOrderInfo mInfo) {
        this.mContext = mContext;
        this.mList = mList;
        this.mHandler = mHandler;
        this.mInfo = mInfo;
        inflater = LayoutInflater.from(mContext);
        mChangClick = (ChangOrderListenner) mContext;
    }

    public interface ChangOrderListenner {
        void changClick(double amount, boolean falg);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.sjt_myorder_details_lv_item, null);

            holder.layoutDelete = (LinearLayout) convertView.findViewById(R.id.layout_delete_order);
            holder.iv_goods_icon = (ImageView) convertView.findViewById(R.id.goods_icon);
            holder.tv_unit_price = (TextView) convertView.findViewById(R.id.tv_unit_price);
            holder.tv_total = (TextView) convertView.findViewById(R.id.tv_total);
            holder.icon_select2 = (TextView) convertView.findViewById(R.id.icon_select2);
            holder.tv_order_num = (TextView) convertView.findViewById(R.id.tv_order_num);
            holder.tv_goods_unit_price = (TextView) convertView.findViewById(R.id.tv_goods_unit_price);
            holder.tv_goods_description = (TextView) convertView.findViewById(R.id.tv_goods_description);
            Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), "iconfont.ttf");
            holder.icon_select2.setTypeface(iconfont);

            holder.countNumber = (EditText) convertView.findViewById(R.id.count_number);
            holder.countAdd = (LinearLayout) convertView.findViewById(R.id.count_add);
            holder.countReduce = (LinearLayout) convertView.findViewById(R.id.count_reduce);
            holder.ll_order = (LinearLayout) convertView.findViewById(R.id.ll_order);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_unit_price.setText(mList.get(position).getUnitPrice());
        //holder.tv_goods_description.setText(mList.get(position).getDescription());
        holder.tv_total.setText(mList.get(position).getTotal());
        String count = mList.get(position).getGoodsQuantity();

        mList.get(position).setCountNumber(Integer.valueOf(count));
        if (null == count || "".equals(count)) {
            holder.countNumber.setText("1");
        } else {
            holder.countNumber.setText(count + "");
        }
        mList.get(position).setSum(0.00);
        mList.get(position).setAmount(0.00);
        //只有当未支付的时候才可以修改订单
        Log.d("Tag", mInfo.getStatus() + "状态");
        if (mInfo.getStatus().equals("1")) {
            holder.tv_order_num.setVisibility(View.GONE);
            holder.ll_order.setVisibility(View.VISIBLE);
            holder.tv_goods_unit_price.setVisibility(View.GONE);
            holder.tv_unit_price.setVisibility(View.VISIBLE);
            holder.icon_select2.setVisibility(View.VISIBLE);
            holder.countAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String mCount = holder.countNumber.getText().toString();
                    int count = Integer.parseInt(mCount);

                    mList.get(position).setSum(Double.valueOf(holder.tv_total.getText().toString()));
                    sum = mList.get(position).getSum();
                    count++;
                    mList.get(position).setGoodsQuantity(count + "");
                    holder.countNumber.setText(mList.get(position).getGoodsQuantity());
                    mList.get(position).setCountNumber(count);
                    String total = Double.valueOf(holder.tv_unit_price.getText().toString()) * Double.valueOf(count) + "";
                    mList.get(position).setTotal(total);
                    holder.tv_total.setText(mList.get(position).getTotal());

                    mList.get(position).setAmount(Double.valueOf(total) - sum);

                    amount = mList.get(position).getAmount();

                    Log.d("Tag", sum + "======" + amount);

                    mChangClick.changClick(amount, true);
                    Message msg = new Message();
                    msg.what = 1;
                    mHandler.sendMessage(msg);
                }
            });
            holder.countReduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String mCount = holder.countNumber.getText().toString();
                    int count = Integer.parseInt(mCount);
                    mList.get(position).setSum(Double.valueOf(holder.tv_total.getText().toString()));
                    sum = mList.get(position).getSum();
                    if (count > 1) {
                        count--;
                        mList.get(position).setGoodsQuantity(count + "");
                        holder.countNumber.setText(mList.get(position).getGoodsQuantity());
                        String total = Double.valueOf(holder.tv_unit_price.getText().toString()) * Double.valueOf(count) + "";
                        mList.get(position).setTotal(total);
                        holder.tv_total.setText(mList.get(position).getTotal());

                        mList.get(position).setAmount(sum - Double.valueOf(total));
                        amount = mList.get(position).getAmount();
                        mChangClick.changClick(amount, false);

                        Message msg = new Message();
                        msg.what = 1;
                        mHandler.sendMessage(msg);
                    } else if (count == 1) {

                        mList.get(position).setGoodsQuantity(count + "");
                        holder.countNumber.setText(mList.get(position).getGoodsQuantity());
                        String total = Double.valueOf(holder.tv_unit_price.getText().toString()) * Double.valueOf(count) + "";
                        mList.get(position).setTotal(total);
                        holder.tv_total.setText(mList.get(position).getTotal());

                        mList.get(position).setAmount(Double.valueOf(holder.tv_unit_price.getText().toString()) * 0);
                        amount = mList.get(position).getAmount();
                        mChangClick.changClick(amount, false);
                        Message msg = new Message();
                        msg.what = 1;
                        mHandler.sendMessage(msg);
                    }
                    //else {
//                    count=0;
//                    mList.get(position).setGoodsQuantity(count+"");
//                    holder.countNumber.setText(mList.get(position).getGoodsQuantity());
//                    String total=Double.valueOf(holder.tv_unit_price.getText().toString())*Double.valueOf(count)+"";
//                    mList.get(position).setTotal(total);
//                    holder.tv_total.setText( mList.get(position).getTotal());
//
//                    mList.get(position).setAmount(sum-Double.valueOf(total));
//                    amount=  mList.get(position).getAmount();
//                    mChangClick.changClick(total,0.00,false);
//                    Message msg = new Message();
//                    msg.what = 1;
//                    mHandler.sendMessage(msg);
//                }
                }
            });
//        if (mList.get(position).isSelect){
//            //选中
//           // holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_select2));
//
//        }else {
//            //未选中
//           // holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_unselected));
//
//        }
//            holder.layoutDelete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mList.get(position).isSelect){
//                    mList.get(position).setSelect(false);
//                   // holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_unselected));
//                }else {
//                    mList.get(position).setSelect(true);
//                    //holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_select2));
//                }
//
//                Message msg = new Message();
//                msg.what = 1;
//                mHandler.sendMessage(msg);
//
//            }
//        });
            //判断是否可删除，只有一个不可点击删除
//        if (mList.size()>1){
//            holder.layoutDelete.setEnabled(false);
//        }else {
//            holder.layoutDelete.setEnabled(true);
//        }
            holder.layoutDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setMessage(mContext.getResources().getString(R.string.error_is_delete_commint));
                    builder.setCancelable(true);
                    builder.setNegativeButton(mContext.getResources().getString(R.string.btn_no), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.setPositiveButton(mContext.getResources().getString(R.string.btn_yes), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (mList.size() > 1) {
                                mChangClick.changClick(Double.valueOf(mList.get(position).getTotal()), false);
                                mList.remove(position);
                                Message msg = new Message();
                                msg.what = 1;
                                mHandler.sendMessage(msg);
                            } else {
                                TipUtil.showToast(mContext.getResources().getString(R.string.error_delete_fail), mContext, 1);
                            }
                            dialog.dismiss();
                        }
                    });
                    builder.setCancelable(false);
                    builder.create().show();

                }
            });
        } else {
            holder.ll_order.setVisibility(View.GONE);
            holder.tv_order_num.setVisibility(View.VISIBLE);
            holder.tv_order_num.setText(mList.get(position).getGoodsQuantity());
            holder.icon_select2.setVisibility(View.GONE);
            holder.tv_goods_unit_price.setVisibility(View.VISIBLE);
            holder.tv_goods_unit_price.setText(mList.get(position).getUnitPrice());
            holder.tv_unit_price.setVisibility(View.GONE);
        }
         Log.d("Tag",mList.get(position).getImage());
        Glide.with(mContext).load(mList.get(position).getImage()).into(holder.iv_goods_icon);
        return convertView;
    }

    class ViewHolder {
        ImageView iv_goods_icon;
        TextView tv_unit_price;
        TextView tv_total;
        TextView icon_select2;
        TextView tv_order_num;
        TextView tv_goods_unit_price;
        TextView tv_goods_description;
        EditText countNumber;
        LinearLayout countAdd;
        LinearLayout countReduce;
        LinearLayout layoutDelete;
        LinearLayout ll_order;
    }
}
